package com.skj.server;

import java.io.* ;

import javax.servlet.http.* ;
import com.google.appengine.api.datastore.* ;
import com.google.appengine.api.users.* ;

@SuppressWarnings("serial")
public class AdminServlet extends HttpServlet
{
	private AdminResDatabase database ;
	
	private String 	resURI ;				// Resource URI
	private String 	resMIMEType ;			// Resource MIME Type
	private Blob	resContent ;			// Resource Content
	private int		resLength ;				// Resource Length in Bytes
	
	public AdminServlet()
	{
		database = new AdminResDatabase() ;
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)throws IOException
	{
		com.google.appengine.api.users.User user ;
		UserService userService = UserServiceFactory.getUserService() ;
		
		String thisURL = req.getRequestURI();
		
		if(userService.isUserLoggedIn() == true)
		{
			if (userService.isUserAdmin() == true)
	        {
				resLength  	= req.getContentLength() ;
				resURI 		= req.getRequestURI() ;
				resMIMEType = req.getContentType() ;
				
				resContent = getContent(req.getInputStream()) ;
				
				database.createTextData(resURI, resMIMEType, resContent) ;
				
				resp.setContentType("text/plain");
				resp.getWriter().println(resLength + " BYTES DATA POSTED");
				resp.getWriter().println(req.getCharacterEncoding()) ;
	        }
	        else // if admin not signed in
	        {
	            resp.getWriter().println("You are not an Administrator. You are not allowed to upload resources");
	        }
		}
		else	// If user is not logged in
		{
			resp.getWriter().println("<p>Please <a href=\"" +
                    userService.createLoginURL(thisURL) +
                    "\">sign in</a>.</p>");
		}
	}
	
	// Retrieves and Creating a user's page
	public void doGet(HttpServletRequest req, HttpServletResponse resp)throws IOException
	{
		com.google.appengine.api.users.User user ;
		UserService userService = UserServiceFactory.getUserService() ;
		
		String thisURL = req.getRequestURI();
		
		if(thisURL.compareTo("/admin") != 0)
		{
			if(userService.isUserLoggedIn() == true)
			{
				if (userService.isUserAdmin() == true)
		        {
					Resource resource = database.retrieveTextData(req.getRequestURI()) ;
					
					if(resource != null)
					{
						resURI		= resource.getLocation() ;
						resMIMEType = resource.getMimeType() ;
						resContent	= resource.getContent() ;
						
						resp.setContentType(resMIMEType);
						resp.getOutputStream().write(resContent.getBytes()) ;
					}
					else
					{
						resp.setContentType("text/plain");
						resp.getWriter().println("No Such Resource Exists");
					}
		        }
			}
		}
		else
		{
			if(userService.isUserLoggedIn() == true)
			{
				if (userService.isUserAdmin() == true)
		        {
		        	user = userService.getCurrentUser() ;
		        	
		        	resp.getWriter().println("Welcome " + user.getNickname());
		        }
		        else // if admin not signed in
		        {
		            resp.getWriter().println("You are not an Administrator. You are not allowed to log in");
		        }
			}
			else	// If user is not logged in
			{
				resp.getWriter().println("<p>Please <a href=\"" +
	                    userService.createLoginURL(thisURL) +
	                    "\">sign in</a>.</p>");
			}
		}   
	}
	
	// Updates a user's data
	public void doPut(HttpServletRequest req, HttpServletResponse resp)throws IOException
	{
		
	}
	
	// Deletes a user
	public void doDelete(HttpServletRequest req, HttpServletResponse resp)throws IOException
	{
		
	}
	
	private Blob getContent(InputStream is)
	{	
		BufferedInputStream bis ;
		byte [] b ;
		
		try
		{
			bis = new BufferedInputStream(is) ;
			b = new byte[resLength] ;
			bis.read(b, 0, resLength) ;
			
			Blob blob = new Blob(b) ;
			
			return blob ;
		}
		catch(Exception e)
		{
			e.printStackTrace() ;
		}
		
		return null ;
	}
}